package cn.tedu.charging.staff.service.impl;

import cn.tedu.charging.common.pojo.param.RoleQuery;
import cn.tedu.charging.common.pojo.po.RoleStaff;
import cn.tedu.charging.common.pojo.po.User;
import cn.tedu.charging.common.pojo.response.JsonResult;
import cn.tedu.charging.common.pojo.vo.RoleVO;
import cn.tedu.charging.staff.feign.RoleClient;
import cn.tedu.charging.staff.feign.UserClient;
import cn.tedu.charging.staff.mapper.StaffMapper;
import cn.tedu.charging.staff.service.StaffService;

import cn.tedu.charging.staff.pojo.param.StaffQuery;
import cn.tedu.charging.staff.pojo.param.StaffSaveParam;
import cn.tedu.charging.staff.pojo.po.Staff;
import cn.tedu.charging.common.pojo.vo.StaffVO;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class StaffServiceImpl implements StaffService {
    @Autowired
    StaffMapper staffMapper;

    public StaffVO login(String tel) {
        log.debug("员工业务层的登录查询角色id的入参为{}", tel);
        StaffVO staffVO = staffMapper.selectByTel(tel);
        log.debug("员工业务层的登录查询角色id的入参为{}出参为{}", tel, staffVO);
//        去角色员工关联表里去查询角色id
        log.debug("员工业务层去角色员工表里去查讯角色id的集合的入参为{}", staffVO.getId());
        List<Long> roleIdList = staffMapper.selectRoleId2RoleStaff(staffVO.getId());
        log.debug("员工业务层去角色员工表里去查讯角色id的集合的入参为{}出参为{}", staffVO.getId(), roleIdList);
//        去权限相关表里去查讯权限id
        log.debug("员工业务层去权限相关表里去查讯权限id的集合的入参为{}", roleIdList);
        for (Long roleId : roleIdList) {
            List<Long> permIdList = staffMapper.selectPermId2RolePerm(roleId);

            staffVO.setPermList(permIdList);
        }
        return staffVO;
    }

//    @Autowired
//    RoleService roleService;
    @Autowired
    RoleClient roleClient;
    @Override
    public List<StaffVO> selectStaff(StaffQuery staffQuery) {
        log.debug("员工业务层查询员工的入参为{}", staffQuery);
        log.debug("员工业务层查询员工调用mapper层的入参为{}", staffQuery);
        List<StaffVO> list = staffMapper.selectStaff(staffQuery);
        List<StaffVO> staffVOS = new ArrayList<>();
        log.debug("获取员工对应的角色");
        log.debug("员工业务层查询员工调用mapper层的出参为{}没有角色集合的员工集合",list);
        for (int i = 0; i < list.size(); i++) {
            log.debug("进入没有角色集合的员工集合的循环");
            StaffVO staffVO = list.get(i);
            log.debug("将没有角色集合的员工集合赋值给staffVO{}",staffVO);
            RoleStaff roleStaff = new RoleStaff();
            roleStaff.setStaffId(staffVO.getId());
            log.debug("调用角色业务层的查找员工角色关联的入参为{}", roleStaff);
            JsonResult<List<RoleStaff>> jsonResult = roleClient.selectRoleStaff(roleStaff);
            List<RoleStaff> list1 = jsonResult.getData();
            log.debug("调用角色业务层的查找员工角色关联的入参为{}出参为员工角色关联集合{}", roleStaff,list1);
            if (list1.size() > 0) {
                RoleQuery roleQuery = new RoleQuery();
                roleQuery.setId(list1.get(0).getRoleId());
                log.debug("调用角色业务层的查找角色的入参为{}",roleQuery);
                JsonResult<List<RoleVO>> jsonResult1 = roleClient.selectRole(roleQuery);
                List<RoleVO> list2 = jsonResult1.getData();
                log.debug("调用角色业务层的查找角色的入参为{}出参为{}",roleQuery,list2);
                StaffVO staffVO1= list.get(i);
                log.debug("将角色集合放入员工vo中");
                List<Long> roleIdList =new ArrayList<>();
                for (int j = 0; j < list2.size(); j++) {
                log.debug("进入循环，这是员工vo对象的角色集合的循环{}",list2);
                    roleIdList.add(list2.get(j).getId());
                }
                staffVO1.setRoleIdList(roleIdList);
                staffVOS.add(staffVO1);
            }
        }
        List<StaffVO> difference = difference(list, staffVOS);
        staffVOS.addAll(difference);
        log.debug("员工业务层查询员工调用mapper层的入参为{}出参为{}", staffQuery, staffVOS);
        return staffVOS;
    }

    /**
     * 将全部集合和含有全部集合的集合进行差集操作
     * @param listAll
     * @param listPart
     * @return
     */
    public static List<StaffVO> difference(List<StaffVO> listAll, List<StaffVO> listPart) {
        log.debug("进入difference方法用来获取未绑定权限集合");
        // 创建一个新的 ArrayList 用于存储结果
        List<StaffVO> result = new ArrayList<>(listAll);

        // 将 listB 转换成一个 Set 用于快速查找
        Set<Long> idsInListB = new HashSet<>();
        for (StaffVO item : listPart) {
            idsInListB.add(item.getId());
        }

        // 移除所有在 listB 中出现的元素
        result.removeIf(item -> idsInListB.contains(item.getId()));

        return result;
    }

//    @Autowired
//    UserMapper userMapper;
    @Autowired
    UserClient userClient;
    @Override
    public void saveStaff(StaffSaveParam staffSaveParam) {
        log.debug("保存员工业务层的入参为{}", staffSaveParam);
//        首先判断是新增还是修改
        if (staffSaveParam.getId() != null) {
            log.debug("这里是更新接口");
            Staff staff = new Staff();
            BeanUtils.copyProperties(staffSaveParam, staff);
            log.debug("保存员工业务层调用修改接口的入参为{}", staff);
            UpdateWrapper<Staff> staffUpdateWrapper = new UpdateWrapper<>();
            staffUpdateWrapper.eq("id", staffSaveParam.getId());
            staffMapper.update(staff, staffUpdateWrapper);

        } else {
            log.debug("这里是新增接口");
            Staff staff = new Staff();
            BeanUtils.copyProperties(staffSaveParam, staff);
            staff.setCreateTime(new Date());
            log.debug("保存员工业务层调用新增接口的入参为{}", staff);
            staffMapper.insert(staff);

//            新增的同时往用户表里页新增一条相同账号的数据
            User user = new User();
            user.setStatus("1");
            user.setUsername(staff.getUsername());
            user.setPassword("123456");
            user.setCreateTime(new Date());
            user.setTel(staff.getTel());
            log.debug("保存员工业务层调用新增用户的接口的入参为{}", user);
            userClient.insertUser(user);
        }
    }

    @Override
    public void deleteStaff(Long staffId) {
        log.debug("删除员工业务层的入参为{}", staffId);
        log.debug("删除员工业务层调用mapper层的入参为{}", staffId);
//        删之前查询员工账号
        StaffQuery staffQuery = new StaffQuery();
        staffQuery.setId(staffId);
        List<StaffVO> staffVOS = selectStaff(staffQuery);
        String tel = staffVOS.get(0).getTel();
        staffMapper.deleteById(staffId);
//        把用户表里的数据也删除

        userClient.deleteUserByStaff(tel);
        log.debug("删除员工业务层调用mapper层的入参为{}没有出参", staffId);
    }

    @Override
    public void updateStatus(Long staffId, String status) {
        log.debug("修改员工状态业务层的入参为{}{}", staffId, status);
        UpdateWrapper<Staff> staffUpdateWrapper = new UpdateWrapper<>();
        staffUpdateWrapper.eq("id", staffId);
        Staff staff = new Staff();
        staff.setStatus(status);
        staff.setUpdateTime(new Date());
        log.debug("修改员工状态业务层调用mapper层的入参为{}{}", staff, staffUpdateWrapper);
        staffMapper.update(staff, staffUpdateWrapper);
        log.debug("修改员工状态业务层调用mapper层的入参为{}{}没有出参", staff, staffUpdateWrapper);

    }
}
